package com.catfive.jet_work.feature.switchpanel

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.FilledIconButton
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.catfive.jet_work.R
import com.catfive.jet_work.core.config.Config.PANEL_LOGIN_INFOS
import com.catfive.jet_work.core.design.component.TopBarIconMenuButton
import com.catfive.jet_work.core.model.PanelLoginInfo
import com.catfive.jet_work.viewmodel.NavigationViewModel

@Composable
fun SwitchPanelRoute(finish: () -> Unit, navigationViewModel: NavigationViewModel) {
    SwitchPanelScreen(finish = finish, navigationViewModel = navigationViewModel)
}


@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun SwitchPanelScreen(
    finish: () -> Unit,
    navigationViewModel: NavigationViewModel
) {
    Scaffold(topBar = {
        TopAppBar(
            colors = TopAppBarDefaults.topAppBarColors(
                containerColor = MaterialTheme.colorScheme.surfaceVariant.copy(0.6f)
            ),
            navigationIcon = {
                TopBarIconMenuButton(
                    painter = painterResource(id = R.drawable.ic_arrow_back),
                    onClick = { finish() }
                )
            },
            title = { Text("选择面板", fontSize = 18.sp) }
        )
    }
    ) { paddingValues ->
        Surface(modifier = Modifier.padding(paddingValues)) {
            LazyVerticalGrid(
                columns = GridCells.Fixed(2), // 设置为2列
                contentPadding = PaddingValues(8.dp),
                verticalArrangement = Arrangement.spacedBy(8.dp),
                horizontalArrangement = Arrangement.spacedBy(8.dp)
            ) {
                items(PANEL_LOGIN_INFOS.size) { index ->
                    GridItem(
                        finish = finish,
                        item = PANEL_LOGIN_INFOS[index],
                        navigationViewModel = navigationViewModel
                    )
                }
            }
        }
    }
}

@Composable
fun GridItem(
    item: PanelLoginInfo,
    navigationViewModel: NavigationViewModel,
    finish: () -> Unit,
) {
    fun goPanel() {
        navigationViewModel.requestPanel(item)
        finish()
    }


    Card(
        modifier = Modifier.height(120.dp),
        colors = CardDefaults.cardColors(containerColor = MaterialTheme.colorScheme.background),
        shape = RoundedCornerShape(15.dp),
        elevation = CardDefaults.cardElevation(defaultElevation = 5.dp), // 默认阴影大小
    ) {
        Column(modifier = Modifier.padding(8.dp)) {
            Text(
                modifier = Modifier.padding(start = 8.dp, top = 8.dp),
                fontSize = 18.sp,
                text = item.name,
                fontWeight = FontWeight.Bold
            )

            Text(
                modifier = Modifier.padding(start = 8.dp, top = 8.dp),
                fontSize = 12.sp,
                text = item.domain, style = MaterialTheme.typography.bodySmall
            )

            Row(modifier = Modifier.padding(top = 16.dp, end = 8.dp)) {
                Spacer(modifier = Modifier.weight(1f))
                FilledIconButton(
                    modifier = Modifier.size(30.dp),
                    onClick = { goPanel() },
                ) {
                    Icon(
                        modifier = Modifier.size(18.dp),
                        painter = painterResource(id = R.drawable.ic_paper_plane),
                        contentDescription = ""
                    )
                }
            }
        }
    }
}